
import 'package:json_annotation/json_annotation.dart';

part 'ResponseBody.g.dart';

@JsonSerializable(explicitToJson: true, genericArgumentFactories: true)
class ResponseBody<T> {

    final String? message;
    final String statusCode;
    final T? data;
    ResponseBody({
        required this.message,
        required this.statusCode,
        T? data
    }) : data = data ?? (_isMapType<T>() ? {} as T : null);

    static bool _isMapType<T>() => T == Map;
    /// Factory constructor to create a ResponseBody from a JSON map.
    /// The [fromJsonT] function is used to convert the JSON data to the generic type [T].
    /// If the generic type [T] is a map, the [fromJsonT] function is not used and an empty map is returned.
    factory ResponseBody.fromJson(Map<String, dynamic> json, T Function(Object? json) fromJsonT) => _$ResponseBodyFromJson<T>(json, fromJsonT);
    /// Converts the ResponseBody to a JSON map.
    /// The [toJsonT] function is used to convert the generic type [T] to a JSON-compatible format.
    /// If [T] is a Map, it will be serialized as an empty map if [data] is null.
    Map<String, dynamic> toJson(Object? Function(T value) toJsonT) => _$ResponseBodyToJson<T>(this, toJsonT);

}